package com.pk.platform.business.charge.web;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pk.platform.business.charge.service.IIncomeExpenseService;
import com.pk.platform.business.charge.vo.IncomeExpenseVO;
import com.pk.platform.business.core.web.GenericAction;
import com.pk.platform.business.data.service.IBaseDataService;
import com.pk.platform.domain.charge.IncomeExpense;
import com.pk.platform.domain.charge.IncomeExpenseIndex;
import com.pk.platform.domain.charge.IncomeExpenseLock;
import com.pk.platform.domain.common.Kindergarten;
import com.pk.platform.domain.data.BaseData;
import com.pk.platform.util.DateConverter;
import com.pk.platform.util.SelectOption;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.constant.ChargeConstant;
import com.pk.platform.util.page.Pager;

public class IncomeExpenseAction extends GenericAction {

	private IncomeExpense ie = new IncomeExpense();
	
	private IncomeExpenseIndex iei = new IncomeExpenseIndex();
	
	private IncomeExpenseLock iel = new IncomeExpenseLock();
	
	private IncomeExpenseVO ievo = new IncomeExpenseVO();
	
	private IIncomeExpenseService incomeExpenseService;
	
	private List<SelectOption> monthList = new ArrayList<SelectOption>();
	
	private String thisYear;

	private String lockMonth;
	
	private List<Map<String, Object>> lockList = new ArrayList<Map<String,Object>>();

	private List<Map<String, Object>> otherYearList = new ArrayList<Map<String,Object>>();
	
	private List<BaseData> indexTypeList = new ArrayList<BaseData>();
	
	private String[] ieIds;
	
	private IBaseDataService baseDataService;
	
	private List<IncomeExpenseIndex> ieiList = new ArrayList<IncomeExpenseIndex>();
	
	private InputStream excelStream;
	
	private boolean existLock;
	
	//action
	
	public String getReportList(){
		existLock = incomeExpenseService.existLock(ievo.getYear(), ievo.getMonth(), kindergartenId);
		ievo.setKindergartenId(kindergartenId);
		lp = incomeExpenseService.getReportList(ievo, pager);
		ievo = incomeExpenseService.getTotalAmount(ievo);
		return SUCCESS;
	}
	
	public String getIncomeExpenseReportExcel(){
		ievo.setKindergartenId(kindergartenId);
		excelStream = incomeExpenseService.getIndexExcelInputStream(ievo);
		return FILE;
	}
	
	public String incomeExpenseCollect()
	{
		if(!StringConverter.isNotNull(thisYear))
		{
			thisYear = DateConverter.getCurrentYearStr();
		}
		monthList = DateConverter.getAllMonthWidthChn(true);
		lockList = incomeExpenseService.queryLockMonth(thisYear, kindergartenId);
		otherYearList = incomeExpenseService.queryYearAgo(kindergartenId, thisYear);
		return SUCCESS;
	}
	
	public String addLock()
	{
		Kindergarten kinder = new Kindergarten(kindergartenId);
		iel.setKindergarten(kinder);
		iel.setYear(thisYear);
		iel.setMonth(lockMonth);
		incomeExpenseService.addLock(iel);
		return SUCCESS;
	}
	
	public String delLock()
	{
		incomeExpenseService.delLock(thisYear, lockMonth, kindergartenId);
		return SUCCESS;
	}
	
	public String toAddIncomeExpense()
	{
		existLock = incomeExpenseService.existLock(ie.getYear(), ie.getMonth(), kindergartenId);
		if(existLock == true)
		{
			message = "该月已锁定。";
			return ERROR;
		}
		indexTypeList = baseDataService.queryBaseDataByParentId(ChargeConstant.INCOME_EXPENSE_TYPE);
		return SUCCESS;
	}
	
	public String addIncomeExpense()
	{
		Kindergarten kinder = new Kindergarten(kindergartenId);
		ie.setKindergarten(kinder);
		incomeExpenseService.addIncomeExpense(ie);
		return SUCCESS;
	}
	
	public String toUpdateIncomeExpense()
	{
		ie = incomeExpenseService.queryIncomeExpenseById(ie.getId());
		indexTypeList = baseDataService.queryBaseDataByParentId(ChargeConstant.INCOME_EXPENSE_TYPE);
		ieiList = incomeExpenseService.queryIncomeExpenseIndexList(ie.getIncomeExpenseIndex().getType().getId());
		return SUCCESS;
	}
	
	public String updateIncomeExpense()
	{
		incomeExpenseService.updateIncomeExpense(ie);
		return SUCCESS;
	}
	
	public String delIncomeExpense()
	{
		if(ieIds != null)
		{
			incomeExpenseService.delIncomeExpense(ieIds);
		}
		return SUCCESS;
	}

	public IncomeExpense getIe() {
		return ie;
	}

	public void setIe(IncomeExpense ie) {
		this.ie = ie;
	}

	public IncomeExpenseIndex getIei() {
		return iei;
	}

	public void setIei(IncomeExpenseIndex iei) {
		this.iei = iei;
	}

	public IncomeExpenseLock getIel() {
		return iel;
	}

	public void setIel(IncomeExpenseLock iel) {
		this.iel = iel;
	}

	public IncomeExpenseVO getIevo() {
		return ievo;
	}

	public void setIevo(IncomeExpenseVO ievo) {
		this.ievo = ievo;
	}

	public IIncomeExpenseService getIncomeExpenseService() {
		return incomeExpenseService;
	}

	public void setIncomeExpenseService(IIncomeExpenseService incomeExpenseService) {
		this.incomeExpenseService = incomeExpenseService;
	}

	public List<SelectOption> getMonthList() {
		return monthList;
	}

	public void setMonthList(List<SelectOption> monthList) {
		this.monthList = monthList;
	}

	public String getThisYear() {
		return thisYear;
	}

	public void setThisYear(String thisYear) {
		this.thisYear = thisYear;
	}

	public List<Map<String, Object>> getLockList() {
		return lockList;
	}

	public void setLockList(List<Map<String, Object>> lockList) {
		this.lockList = lockList;
	}

	public String getLockMonth() {
		return lockMonth;
	}

	public void setLockMonth(String lockMonth) {
		this.lockMonth = lockMonth;
	}

	public List<Map<String, Object>> getOtherYearList() {
		return otherYearList;
	}

	public void setOtherYearList(List<Map<String, Object>> otherYearList) {
		this.otherYearList = otherYearList;
	}

	public List<BaseData> getIndexTypeList() {
		return indexTypeList;
	}

	public void setIndexTypeList(List<BaseData> indexTypeList) {
		this.indexTypeList = indexTypeList;
	}

	public String[] getIeIds() {
		return ieIds;
	}

	public void setIeIds(String[] ieIds) {
		this.ieIds = ieIds;
	}

	public IBaseDataService getBaseDataService() {
		return baseDataService;
	}

	public void setBaseDataService(IBaseDataService baseDataService) {
		this.baseDataService = baseDataService;
	}

	public List<IncomeExpenseIndex> getIeiList() {
		return ieiList;
	}

	public void setIeiList(List<IncomeExpenseIndex> ieiList) {
		this.ieiList = ieiList;
	}

	public InputStream getExcelStream() {
		return excelStream;
	}

	public void setExcelStream(InputStream excelStream) {
		this.excelStream = excelStream;
	}

	public boolean isExistLock() {
		return existLock;
	}

	public void setExistLock(boolean existLock) {
		this.existLock = existLock;
	}
	
}
